package com.wonderful.eyepetizer.ui.pages.detail

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.app.ktx.loadMoreView
import com.wonderful.eyepetizer.ui.pages.daily.RankItemWidget
import com.wonderful.eyepetizer.ui.pages.detail.viewmodel.CategoryDetailViewModel
import dagger.hilt.android.AndroidEntryPoint
import me.onebone.toolbar.CollapsingToolbarScaffold
import me.onebone.toolbar.ScrollStrategy
import me.onebone.toolbar.rememberCollapsingToolbarScaffoldState

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/22 14:56
 */
@AndroidEntryPoint
class CategoryDetailActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor = android.graphics.Color.TRANSPARENT
        WindowCompat.setDecorFitsSystemWindows(window, false)
        val categoryId = intent.getIntExtra(KEY_CATEGORY_ID, 0)
        val categoryName = intent.getStringExtra(KEY_CATEGORY_NAME) ?: ""
        val headImage = intent.getStringExtra(KEY_HEAD_IMAGE) ?: ""
        setContent {
            CategoryDetailPage(
                categoryName = categoryName,
                headImage = headImage
            )
        }
    }

    companion object {
        const val KEY_CATEGORY_ID = "category_id"
        const val KEY_CATEGORY_NAME = "category_name"
        const val KEY_HEAD_IMAGE = "head_image"
        fun actionStart(
            context: Context,
            categoryId: Int,
            categoryName: String,
            headImage: String
        ) {
            val intent = Intent(context, CategoryDetailActivity::class.java)
            intent.putExtra(KEY_CATEGORY_ID, categoryId)
            intent.putExtra(KEY_CATEGORY_NAME, categoryName)
            intent.putExtra(KEY_HEAD_IMAGE, headImage)
            context.startActivity(intent)
        }
    }
}

@Composable
fun CategoryDetailPage(categoryName: String, headImage: String) {
    val viewModel: CategoryDetailViewModel = viewModel()
    val lazyPagingItems = viewModel.categoryPageFlow.collectAsLazyPagingItems()
    val context = LocalContext.current
    val state = rememberCollapsingToolbarScaffoldState()
    CollapsingToolbarScaffold(
        modifier = Modifier.fillMaxSize(),
        state = state,
        scrollStrategy = ScrollStrategy.ExitUntilCollapsed,
        toolbarModifier = Modifier.background(MaterialTheme.colors.primary),
        toolbar = {
            Image(
                painter = rememberAsyncImagePainter(model = headImage),
                contentDescription = null,
                modifier = Modifier
                    .parallax(0.5f)
                    .fillMaxWidth()
                    .height(200.dp)
                    .graphicsLayer {
                        alpha = state.toolbarState.progress
                    },
                contentScale = ContentScale.Crop
            )
            Row(
                modifier = Modifier
                    .statusBarsPadding()
                    .fillMaxWidth()
                    .height(50.dp)
                    .pin()
            ) {
                IconButton(onClick = { if (context is Activity) context.finish() }) {
                    Icon(
                        imageVector = Icons.Outlined.ArrowBack,
                        contentDescription = null,
                        tint = Color.White
                    )
                }
            }
            Text(
                text = categoryName,
                style = TextStyle(
                    fontSize = (18 + (30 - 18) * state.toolbarState.progress).sp,
                    color = Color.White
                ),
                modifier = Modifier
                    .road(Alignment.CenterStart, Alignment.BottomStart)
                    .padding(40.dp, 16.dp, 16.dp, 16.dp)
                    .statusBarsPadding()
            )
        }
    ) {
        LazyColumn(modifier = Modifier.fillMaxSize()) {
            items(lazyPagingItems.itemCount) { index ->
                val pagingItem = lazyPagingItems[index]
                pagingItem?.data?.run {
                    RankItemWidget(itemData = this)
                }
            }
            loadMoreView(pagingItems = lazyPagingItems)
        }
    }
}